package quantity.trande

import cn.hutool.core.date.DateUtil
import cn.hutool.core.util.RandomUtil
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONPath
import okhttp3.Request
import okhttp3.Response
import okhttp3.WebSocket
import okhttp3.WebSocketListener
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener
import okhttp3.sse.EventSources.createFactory
import okio.ByteString
import quantity.trande.util.executeInsert
import java.math.BigDecimal
import java.math.RoundingMode
import java.nio.charset.StandardCharsets
import java.util.*

fun main() {

//    Accept-Encoding: gzip, deflate, br
//Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
//Cache-Control: no-cache
//Connection: Upgrade
//Host: data.tradingview.com
//Origin: https://www.tradingview.com
//Pragma: no-cache
//Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
//Sec-WebSocket-Key: pnqY1hMTMSg8G5bsSGRymQ==
//Sec-WebSocket-Version: 13
//Upgrade: websocket
//User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.42
    val request: Request = Request.Builder()
        .get()
        .url("wss://data.tradingview.com/socket.io/websocket?from=chart%2F&date=2022_11_17-14_02")
        .header("Accept-Language","zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
        .header("Cache-Control","no-cache")
        .header("Connection","Upgrade")
        .header("Host","data.tradingview.com")
        .header("Origin","https://www.tradingview.com")
        .header("Pragma","no-cache")
//        .header("Sec-WebSocket-Extensions","permessage-deflate; client_max_window_bits")
        .header("Sec-WebSocket-Key","pnqY1hMTMSg8G5bsSGRymQ==")
//        .header("Sec-WebSocket-Version","13")
        .header("Upgrade","websocket")
        .header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.42")
        .build()
    val newWebSocket = CLIENT.newWebSocket(request, object : WebSocketListener() {
        override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
            super.onClosed(webSocket, code, reason)
        }

        override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
            super.onClosing(webSocket, code, reason)
        }

        override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
            super.onFailure(webSocket, t, response)
        }

        override fun onMessage(webSocket: WebSocket, text: String) {
            println(text)
        }

        override fun onMessage(webSocket: WebSocket, bytes: ByteString) {
            super.onMessage(webSocket, bytes)
        }

        override fun onOpen(webSocket: WebSocket, response: Response) {
            super.onOpen(webSocket, response)
        }
    })
    newWebSocket.send("")

}